package com.springboot.demo.controller;

import com.springboot.demo.entity.*;
import com.springboot.demo.service.AlarmService;
import com.springboot.demo.utils.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Update;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-09-10 11:01
 */
@RestController
@Api(tags = "报警接口")
public class AlarmController {
    @Autowired
    ResponseUtils responseUtils;
    @Autowired
    AlarmService alarmService;

    @GetMapping("/history/data")
    @ApiOperation(value = "获取历史数据信息")
    public ResponseUtils<List<HistoryData>> getHistoryDataInfo(
            @RequestParam(value = "sceneId", required = false) @ApiParam("场景id，用在场景仪表盘获取场景报警数据") Long sceneId,
            @RequestParam(value = "startDate", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
            @RequestParam(value = "endDate", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
            @RequestParam(value = "sort", required = false) @ApiParam(
                    "排序字段，格式：列名+空格+desc或者asc,可排序字段：" +
                            "{\"varName\",\"deviceName\",\"sceneName\",\"dataType\",\"value\",\"productTime\"}")
            @Length(max = 32, message = "排序字段长度超过限制值") String sort,
            @RequestParam(value = "search", required = false) @ApiParam("1~32位")
            @Length(max = 32, message = "搜索字段长度超过限制值") String search,
            @Valid PageRequest request
    ) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        PageResult historyDataInfo = alarmService.getHistoryDataInfo(implUser.getId(), sceneId, startDate, endDate, sort,
                search, request);
        return responseUtils.buildSuccessResponse(historyDataInfo);
    }

    @GetMapping("/history/alarm")
    @ApiOperation(value = "获取历史报警信息")
    public ResponseUtils<List<HistoryAlarm>> getHistoryAlarmInfo(
            @RequestParam(value = "sceneId", required = false) @ApiParam("场景id，用在场景仪表盘获取场景报警数据") Long sceneId,
            @RequestParam(value = "startDate", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
            @RequestParam(value = "endDate", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
            @RequestParam(value = "sort", required = false)
            @ApiParam("排序字段，格式：列名+空格+desc或者asc,可排序字段： {\"varName\", \"deviceName\", \"sceneName\", \"dataType\", " +
                    "\"value\", \"productTime\", \"alarmType\", \"alarmText\"")
            @Length(max = 32, message = "排序字段长度超过限制值") String sort,
            @RequestParam(value = "search", required = false) @ApiParam("1~32位")
            @Length(max = 32, message = "搜索字段长度超过限制值") String search,
            @Valid PageRequest request
    ) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        PageResult historyAlarmInfo = alarmService.getHistoryAlarmInfo(implUser.getId(), sceneId, startDate, endDate,
                sort, search, request);
        return responseUtils.buildSuccessResponse(historyAlarmInfo);
    }

    @GetMapping("/history/record/count")
    @ApiOperation(value = "获取历史变量和报警条数信息")
    public ResponseUtils getHistoryAlarmCount() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        Integer historyAlarmCount = alarmService.getHistoryAlarmCount(implUser.getId());
        return responseUtils.buildSuccessResponse(historyAlarmCount);
    }

    @PutMapping("/history/record/user/{userId}/count/{count}")
    @ApiOperation(value = "设置历史变量和报警条数信息", notes = "超级管理员权限")
    public ResponseUtils setHistoryAlarmCount(
            @PathVariable(value = "userId") @ApiParam(value = "用户id",required = true) Long userId,
            @PathVariable(value = "count") @ApiParam(value = "记录条数",required = true) @Size(min = 0) Integer count) {
        alarmService.setHistoryAlarmCount(userId, count);
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/history/data")
    @ApiOperation(value = "删除历史变量")
    public ResponseUtils deleteHistoryData(
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @ApiParam("起始日期") Date startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @ApiParam("结束日期") Date endDate) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        alarmService.deleteHistoryData(implUser.getId(), startDate, endDate);
        return responseUtils.buildSuccessResponse();
    }

    @DeleteMapping("/history/alarm")
    @ApiOperation(value = "删除历史报警")
    public ResponseUtils deleteHistoryAlarm(
            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @ApiParam("起始日期") Date startDate,
            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            @ApiParam("结束日期") Date endDate) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        alarmService.deleteHistoryAlarm(implUser.getId(), startDate, endDate);
        return responseUtils.buildSuccessResponse();
    }
}
